# -*- coding:UTF-8 -*-

import os
import git.cmd
from git.repo import Repo

#todo： 定义克隆远程仓库URL及本地目录
# REMOTE_URL = "http://账号:密码@192.168.10.8:7990/scm/bambooscript/test.git"
REMOTE_URL = "https://johnforrest:yin0503@gitee.com/johnforrest/mapboxdemo.git"
dir_path = os.path.join(r"E:\devcode", "mapboxdemo")
# 账号和密码
git_user = "johnforrest"
git_email = "571716604@qq.com"


# 判断仓库本地目录是否存在
def is_dir(path):
    result = os.path.isdir(path + os.sep + ".git")
    if not result:
        print("路径:%s \n是否存在:" % s % (path, result))
        return result
    else:
        print("路径:%s \n是否存在: %s" % (path, result))
        return result


# 获取对象。path是仓库到本地的地址
def git_object(path):
    # 获取repo对象。本地仓库地址
    repo = Repo(path)
    # 通过Repo对象获取git对象
    git = repo.git
    # 通过repo对象获取remote对象
    remote = repo.remote()
    return git, remote


# 克隆远程仓库到本地
def git_clone(remote_url, local_path):
    dir_msg = is_dir(dir_path)
    if not dit_msg:
        Repo.clone_from(remote_url, to_path=local_path)
        print("已克隆到本地")


# 添加提交用户 邮箱
def git_user(user, user_email):
    repo = Repo(dir_path)
    git = repo.git()
    git.config("--global", "user.name", "%s" % (user))
    git.config("--global", "user.email", "%s" % (user_email))


# pull-add-commit-push。 commit_msg表示提交信息
def git_push(path, commit_msg):
    git, remote = git_object(path)
    remote.pull()
    git.add('.')
    git.commit('-m', '%s' % (commit_msg))
    remote.push()


git_push(dir_path,"测试提交")
# git,remote=git_object(dir_path)
# log_msg=git.log()
# print (log_msg)
# 克隆到本地
# git_clone(REMOTE_URL, dir_path)
